package summer.backtracking;

import java.util.ArrayList;
import java.util.List;

public class Solution216 {
    List<List<Integer>> res = new ArrayList<>();
    List<Integer> tmp = new ArrayList<>();

    public List<List<Integer>> combinationSum3(int k, int n) {
        backtracking(n, k, 1, 0);
        return res;
    }

    public void backtracking(int n, int k, int index, int sum) {
        if (tmp.size() == k) {
            if (sum == n) {
                res.add(new ArrayList<>(tmp));
            }
            return;
        }
        for (int i = index; i < 10; i++) {
            tmp.add(i);
            sum += i;
            backtracking(n, k, i + 1, sum);
            sum -= i;
            tmp.remove(tmp.size() - 1);
        }
    }
}
